/*
 * Copyright (C) 2004-2007  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"

#include <malloc.h>
#include <string.h>

#include "FdoWcsGetCoverageDescription.h"
#include "FdoWcsCoverageDescriptions.h"
#include "FdoWcsCoverageDescription.h"

FdoWcsGetCoverageDescription::FdoWcsGetCoverageDescription (FdoIConnection *connection) :
    FdoWcsCommand<FdoWcsIGetCoverageDescription> (connection)
{
}

FdoWcsGetCoverageDescription::~FdoWcsGetCoverageDescription (void)
{
}

// <summary>Dispose this object.</summary>
// <returns>Returns nothing.</returns> 
void FdoWcsGetCoverageDescription::Dispose ()
{
    delete this;
}

/// <summary> Gets the name of the FeatureClass for which the relevant coverage description will be retrieved.</summary>
/// <returns> Returns the FeatureClass name </returns> 
FdoString* FdoWcsGetCoverageDescription::GetFeatureClassName()
{
    return (mFeatureClassName);
}

/// <summary> 
/// Sets the name of the FeatureClass for which the relevant coverage
/// description will be retrieved. This function is mandatory; if not specified, 
/// execution of the command will throw an exception. 
/// </summary>
/// <param name="value">Input the FeatureClass name</param> 
/// <returns>Returns nothing</returns> 
void FdoWcsGetCoverageDescription::SetFeatureClassName(FdoString* value)
{
    mFeatureClassName = value;
}

/// <summary> 
/// Executes the FdoWcsGetCoverageDescription command and returns a 
/// FdoWcsCoverageDescription relevant with the feature class.
/// </summary>
/// <returns> 
/// Returns FdoWcsCoverageDesctiprion that are relevant with the specified
/// FeatureClass.
/// </returns> 
FdoWcsCoverageDescription* FdoWcsGetCoverageDescription::Execute ()
{
    VALIDATE_ARGUMENT(mFeatureClassName);

	FdoStringsP identifiers = FdoStringCollection::Create();
	FdoStringP identifier;
	FdoDictionaryP namedCoverageMappings = mConnection->GetNamedCoverageMappings();
	FdoDictionaryElementP coverageElement = namedCoverageMappings->GetItem(mFeatureClassName);
	if (coverageElement)
	{
		identifier = coverageElement->GetValue();
		identifiers->Add (identifier);
	}
	else
		throw FdoSchemaException::Create (NlsMsgGet (FDOWCS_12001_COVERAGE_NOT_EXIST, "The WCS coverage '%1$ls' does not exist.", (FdoString*)mFeatureClassName));
	
	FdoWcsCoverageDescriptionsP coverageDescs = mConnection->GetCoverageDescriptions(identifiers);
    
	return FDO_SAFE_ADDREF(coverageDescs->GetItem(0));
}